package cn.sunshuai.xhr;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class Producer {
    public static void main(String[] args) throws Exception {
        //创建生产者组
        DefaultMQProducer producer = new DefaultMQProducer("xhr-producer-group");
        //设置nameserver地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        //开启
        producer.start();
        //创建消息
        Message message = new Message("topic", "pet", "今天是个好日子".getBytes(RemotingHelper.DEFAULT_CHARSET));
        //发送消息
        producer.send(message, new SendCallback() {
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }

            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("发送消息失败了");
            }
        });
        System.out.println("生产者");

        //关闭
        //producer.shutdown();
    }

}
